package city.spring.modules.medicine.service.impl;

import city.spring.modules.medicalstation.drugwarehouse.service.DrugwarehouseService;
import city.spring.modules.medicine.entity.MedicinedeliveryMedicineExtEntity;
import city.spring.modules.medicine.repository.MedicinedeliveryMedicineExtRepository;
import city.spring.modules.medicine.service.MedicinedeliveryMedicineExtService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：药品出库与药品关联表
 *
 * @author 谢金婷
 * @date 2020-07-08 16:17:05
 */
@CacheConfig(cacheNames = {MedicinedeliveryMedicineExtServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MedicinedeliveryMedicineExtServiceImpl extends ServiceImpl<MedicinedeliveryMedicineExtRepository, MedicinedeliveryMedicineExtEntity> implements MedicinedeliveryMedicineExtService {

    private final MedicinedeliveryMedicineExtRepository deliveryMedicineExtRepository;
    private final DrugwarehouseService medicinewarehouseService;

    public MedicinedeliveryMedicineExtServiceImpl(MedicinedeliveryMedicineExtRepository deliveryMedicineExtRepository, DrugwarehouseService medicinewarehouseService) {
        this.deliveryMedicineExtRepository = deliveryMedicineExtRepository;
        this.medicinewarehouseService = medicinewarehouseService;
    }

    @Override
    public void saveMedicinedeliveryMedicineExt(MedicinedeliveryMedicineExtEntity entity) {

//        // 新增医护站入库记录 每一种药品对应一条入库记录
//        //1.查出出库的药品信息
//        List<MedicinedeliveryMedicineExtEntity> deliveryMedicines = deliveryMedicineExtRepository.getDeliveryMts(entity.getMedicinedeliveryId());
//        System.out.println("deliveryMedicines :"+deliveryMedicines);
//        MedicalstationMedicinewarehouseEntity medicinewarehouseEntity = new MedicalstationMedicinewarehouseEntity();
//        medicinewarehouseEntity.setDeliveryId(entity.getMedicinedeliveryId());
//        medicinewarehouseEntity.setMedicineId(entity.getMedicineId());
////        medicinewarehouseEntity.setStorehouseId(entity.getWstorehouseId());
//        medicinewarehouseEntity.setWarehousingdate(new Date());
//        medicinewarehouseEntity.setApprovalStatus(1);
//        medicinewarehouseService.saveMedicalstationMedicinewarehouse(medicinewarehouseEntity);


        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMedicinedeliveryMedicineExt(MedicinedeliveryMedicineExtEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMedicinedeliveryMedicineExt(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMedicinedeliveryMedicineExt(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

}